//基础公用方法
/* eslint-disable no-unused-vars */

import { ConvertCommonReturn } from '/@/Modal/CommonReturn';
import { ElMessage } from 'element-plus';

// 接口类型和方法
interface BaseType{

    //对象是否可用
    objectAvailable(input: Object):boolean

    //对象是否不可用
    objectUnavailable(input: Object):boolean

    readonly YYYYMMDDHH24MISS: number
    readonly YYYY_MM_DDHH24MISS: number
    readonly YYYY: number
    readonly YYYYMM: number
    readonly YYYYMMDD: number
    readonly YYYY1MM: number
    readonly YYYY1MM1DD: number
    readonly YYYY_MM: number
    readonly YYYY_MM_DD: number

    gmtToString(gmt: Date, dateType?: number): string

    copyObject(newObj: any, oldObj: any)

    getDrugstoreResult(result: Object): Object

    getType(o)
}



class Common implements BaseType{

    objectAvailable(input: any) {
        if (typeof input == "undefined" || input == null || input == '') {
            return false
        } else {
            return true
        }
    }

    objectUnavailable(input: any) {
        if (typeof input == "undefined" || input == null || input == '') {
            return true
        } else {
            return false
        }
    }

    YYYYMMDDHH24MISS: number
    YYYY_MM_DDHH24MISS: number
    YYYY: number
    YYYYMM: number
    YYYYMMDD: number
    YYYY1MM: number
    YYYY1MM1DD: number
    YYYY_MM: number
    YYYY_MM_DD: number
    /**
     * 将GMT时间格式转换成字符串
     * @param gmt Date GMT格式的时间
     * @param dateType number 目标时间字符串格式 不输入就是默认
     *                        默认                 yyyy/mm/dd hh24:mi:ss
     *                        YYYYMMDDHHMISS      yyyymmddhh24miss
     *                        YYYY_MM_DDHH24MISS  yyyy-mm-dd hh24:mi:ss
     *                        YYYY                yyyy
     *                        YYYYMM              yyyymm
     *                        YYYYMMDD            yyyymmdd
     *                        YYYY1MM             yyyy/mm
     *                        YYYY1MM1DD          yyyy/mm/dd
     *                        YYYY_MM             yyyy-mm
     *                        YYYY_MM_DD          yyyy-mm-dd
     * @returns 时间字符串
     */
    gmtToString(gmt: Date, dateType?: number) {
        if (this.objectUnavailable(gmt)) {
            return null
        }
        let date = new Date(gmt)
        let Str=date.getFullYear().toString()
        if (this.objectUnavailable(dateType)) {
            Str = Str + '/' +
            (this.handleTimeStr(date.getMonth() + 1)) + '/' +
            this.handleTimeStr(date.getDate()) + ' ' +
            this.handleTimeStr(date.getHours()) + ':' +
            this.handleTimeStr(date.getMinutes()) + ':' +
            this.handleTimeStr(date.getSeconds())
        } else {
            switch(dateType) {
                case this.YYYYMMDDHH24MISS:
                    Str = Str +
                    (this.handleTimeStr(date.getMonth() + 1)) +
                    this.handleTimeStr(date.getDate()) +
                    this.handleTimeStr(date.getHours()) +
                    this.handleTimeStr(date.getMinutes()) +
                    this.handleTimeStr(date.getSeconds())
                    break;
                case this.YYYY_MM_DDHH24MISS:
                    Str = Str + '-' +
                    (this.handleTimeStr(date.getMonth() + 1)) + '-' +
                    this.handleTimeStr(date.getDate()) + ' ' +
                    this.handleTimeStr(date.getHours()) + ':' +
                    this.handleTimeStr(date.getMinutes()) + ':' +
                    this.handleTimeStr(date.getSeconds())
                    break;
                case this.YYYY:
                    break;
                case this.YYYYMM:
                    Str = Str +
                    (this.handleTimeStr(date.getMonth() + 1))
                    break;
                case this.YYYYMMDD:
                    Str = Str +
                    (this.handleTimeStr(date.getMonth() + 1)) +
                    this.handleTimeStr(date.getDate())
                    break;
                case this.YYYY1MM:
                    Str = Str + '/' +
                    (this.handleTimeStr(date.getMonth() + 1))
                    break;
                case this.YYYY1MM1DD:
                    Str = Str + '/' +
                    (this.handleTimeStr(date.getMonth() + 1)) + '/' +
                    this.handleTimeStr(date.getDate())
                    break;
                case this.YYYY_MM:
                    Str = Str + '-' +
                    (this.handleTimeStr(date.getMonth() + 1))
                    break;
                case this.YYYY_MM_DD:
                    Str = Str + '-' +
                    (this.handleTimeStr(date.getMonth() + 1)) + '-' +
                    this.handleTimeStr(date.getDate())
                    break;
                default:
                    Str = Str + '/' +
                    (this.handleTimeStr(date.getMonth() + 1)) + '/' +
                    this.handleTimeStr(date.getDate()) + ' ' +
                    this.handleTimeStr(date.getHours()) + ':' +
                    this.handleTimeStr(date.getMinutes()) + ':' +
                    this.handleTimeStr(date.getSeconds())
            }
        }
        return Str
    }

    
    private handleTimeStr(str: number){  // 解决 10:2:2 出现各位数字格式化为 10:02:02
        if (str < 10){
            return '0'+ str
        }else {
            return str
        }
    }

    /**
     * 对对象进行深复制
     * @param {*} originObject 原对象
     * @returns 复制的对象
     */
    copyObject(newObj: any, oldObj: any) {
        for (let key in oldObj) {
            let value = oldObj[key];
            // 1. 如果是数组
            if (value instanceof Array) {
                newObj[key] = [];
                this.copyObject(newObj[key],value);
                // 2. 如果是对象(数组也是对象所有要写在前头)
            }else if (value instanceof Object) {
                newObj[key] = {};
                this.copyObject(newObj[key],value);
                //3. 普通数据类型
            }else {
                newObj[key] = value;
            }
        }
    }

    /**
     * 获取自建云药店相关后台数据，同时做异常处理
     * @param {*} result 返回对象
     * @returns 处理后正常的数据
     */
    getDrugstoreResult(result: Object): Object{
        const commonReturn = ConvertCommonReturn.toCommonReturn(result)
        if (commonReturn.successFlag == -1) {
            ElMessage({
                showClose: true,
                duration: 5000,
                message: commonReturn.errorMessage,
                type: 'error',
            })
            return null
        }
        return commonReturn.data
    }

    /**
     * 判断对象类型
     * @param {*} o 原对象
     * @returns 对象类型
     */
    getType(o){
        var _t;
        return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
    }

}

const common = new Common()
common.YYYYMMDDHH24MISS=1
common.YYYY_MM_DDHH24MISS=2
common.YYYY=3
common.YYYYMM=4
common.YYYYMMDD=5
common.YYYY1MM=6
common.YYYY1MM1DD=7
common.YYYY_MM=8
common.YYYY_MM_DD=9
export default common